﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://wpcontrol.sourceforge.net/ for more.
//
// Copyright (c) 2014 nscrolldev
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;

namespace WallpaperControl
{
    /// <summary>
    /// A class that models images
    /// </summary>
    class Image
    {
        /// <summary>
        /// Returns an array with the supported image extensions (includs leading dot)
        /// </summary>
        private static string[] SupportedImages
        {
            get
            {
                if (Program.HasAdvancedImgSupport())
                {
                    return new string[] { ".jpg", ".jpeg", ".jpe", ".jfif", ".bmp", ".dib", ".png", ".gif", ".tiff", ".tif", ".raw" };
                }
                else
                {
                    return new string[] { ".bmp", ".dib" };
                }
            }
        }

        /// <summary>
        /// Stores the path of the image
        /// </summary>
        private string _path;

        /// <summary>
        /// Checks if the image with the extension is supported by the application.
        /// </summary>
        /// <returns>True if the image is supported, false otherwise.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public bool IsSupported()
        {
            string extension = System.IO.Path.GetExtension(this._path);
            if (!String.IsNullOrEmpty(extension))
            {
                bool isSupported = false;
                foreach (string supportedExt in SupportedImages)
                {
                    if (extension == supportedExt || extension.ToLower() == supportedExt)
                    {
                        isSupported = true;
                    }
                }
                return isSupported;
            }
            else
            {
                throw new ArgumentNullException("extension", "The extension is not set.");
            }
        }

        /// <summary>
        /// Creates a new image object
        /// </summary>
        /// <param name="path">The file path to the image</param>
        public Image(string path)
        {
            this._path = path;
        }

        /// <summary>
        /// Opens the image with the associated application.
        /// </summary>
        /// <exception cref="System.ComponentModel.Win32Exception"></exception>
        /// <exception cref="System.ObjectDisposedException"></exception>
        /// <exception cref="System.IO.FileNotFoundException"></exception>
        public void Preview()
        {
            // Open image with associated program
            Process.Start(this._path);
        }
    }
}
